#include "connectmanager.h"
#include "form/homewindow.h"
#include "form/netmanager.h"
#include "util/realdatamodel.h"
#include "slotadpter/mainslotadpter.h"
#include "form/formmanager.h"
#include "form/setting.h"
#include "form/actiondebugform.h"

#include <QAbstractSocket>


ConnectManager::ConnectManager(HomeWindow* _homeWindow,
                               QSharedPointer<FormManager> _formManager,
                               QSharedPointer<AdpterManager> _adpManager,
                               QObject *parent) : BaseObject(parent), homeWindow(_homeWindow),
                                                  formManager(_formManager), adpterManager(_adpManager)
{

    leftBaseTcp = QSharedPointer<BaseTcp>(new BaseTcp());
    leftThreadTasks = QSharedPointer<ThreadTasks>(new ThreadTasks());
    leftReceiveTask = QSharedPointer<QThread>(new QThread());

    rightBaseTcp = QSharedPointer<BaseTcp>(new BaseTcp());
    rightThreadTasks = QSharedPointer<ThreadTasks>(new ThreadTasks());
    rightReceiveTask = QSharedPointer<QThread>(new QThread());

    initConnect();
}

ConnectManager::~ConnectManager()
{
    formManager.reset();
    adpterManager.reset();
}

void ConnectManager::connnectToNetSolts(QString address,  int port)
{
    leftConnectToSocket(address, port);
}

void ConnectManager::leftConnectToSocket(QString ip, int port)
{
    emit leftConnectSignal(ip,port);
}

void ConnectManager::rightConnectToSocket(QString ip, int port)
{
    emit rightConnectSignal(ip,port);
}

void ConnectManager::initConnect()
{

    initNetAndThreadConnect();
    initConnectNetAndStatus();
    initSendCommandConnect();

}

void ConnectManager::initNetAndThreadConnect()
{
    ////左控制器
    connect(leftBaseTcp.get(), SIGNAL(emitData(QByteArray)),
            leftThreadTasks.get(), SLOT(onReceiveFromTcp(QByteArray)),
            Qt::QueuedConnection);
    connect(this, SIGNAL(leftConnectSignal(QString,int)),
            leftBaseTcp.get(), SLOT(onConnect(QString,int)));
    connect(leftThreadTasks.get(), SIGNAL(emitHandle(QByteArray)),
            homeWindow, SLOT(on_receive_handle_data(QByteArray)),
            Qt::QueuedConnection);
    connect(leftThreadTasks.get(), SIGNAL(notifyMainWindowSignal(RealDataModel)),
            homeWindow, SLOT(onReceiveResult(RealDataModel)),
            Qt::QueuedConnection);

    //tcp 放到单独的线程中运行
    leftBaseTcp->moveToThread(leftReceiveTask.get());
    leftReceiveTask->start();

    //
    leftThreadTasks->setTcp(leftBaseTcp.get());
    leftThreadTasks->start();

    ////右控制器
    connect(rightBaseTcp.get(), SIGNAL(emitData(QByteArray)),
            rightThreadTasks.get(), SLOT(onReceiveFromTcp(QByteArray)),
            Qt::QueuedConnection);
    connect(this, SIGNAL(rightConnectSignal(QString,int)),
            rightBaseTcp.get(), SLOT(onConnect(QString,int)), Qt::QueuedConnection);
    connect(rightThreadTasks.get(), SIGNAL(emitHandle(QByteArray)),
            homeWindow, SLOT(on_receive_handle_data(QByteArray)),
            Qt::QueuedConnection);
    connect(rightThreadTasks.get(), SIGNAL(notifyMainWindowSignal(RealDataModel)),
            homeWindow, SLOT(onReceiveResult(RealDataModel)),
            Qt::QueuedConnection);

    rightBaseTcp->moveToThread(rightReceiveTask.get());
    rightReceiveTask->start();
    rightThreadTasks->setTcp(rightBaseTcp.get());
    rightThreadTasks->start();
}


void ConnectManager::initConnectNetAndStatus()
{
    connect(formManager->netManager.get(), SIGNAL(connectToNetSignals(QString, int)),
            leftBaseTcp.get(), SLOT(onConnect(QString, int)),
            Qt::QueuedConnection);
    connect(formManager->netManager.get(), SIGNAL(connectToNet2000Signals(QString, int)),
            rightBaseTcp.get(), SLOT(onConnect(QString, int)),
            Qt::QueuedConnection);

    connect(formManager->netManager.get(), SIGNAL(netDisconnect()),
            leftBaseTcp.get(), SLOT(onDisconnect()), Qt::QueuedConnection);
    connect(formManager->netManager.get(), SIGNAL(net2000Disconnect()),
            rightBaseTcp.get(), SLOT(onDisconnect()), Qt::QueuedConnection);


    connect(leftBaseTcp.get(), SIGNAL(netStateSignals(int)),
            formManager->netManager.get(), SLOT(netStateSlots(int)), Qt::QueuedConnection);

    connect(leftBaseTcp.get(), SIGNAL(disconnected()),
            formManager->netManager.get(), SLOT(netDisconnected()), Qt::QueuedConnection);

    connect(leftBaseTcp.get(), SIGNAL(connected()),
            formManager->netManager.get(), SLOT(netConnected()), Qt::QueuedConnection);


    /// 网络连接管理。
    connect(leftBaseTcp.get(), SIGNAL(netStateSignals(int)),
            adpterManager->mainSlotAdpter.get(), SLOT(netStateSlots(int)), Qt::QueuedConnection);

    connect(leftBaseTcp.get(), SIGNAL(disconnected()),
            (adpterManager->mainSlotAdpter).get(), SLOT(netDisconnected()), Qt::QueuedConnection);

    connect(leftBaseTcp.get(), SIGNAL(connected()),
            (adpterManager->mainSlotAdpter).get(), SLOT(netConnected()), Qt::QueuedConnection);

}

void ConnectManager::initSendCommandConnect()
{
    connect(this, SIGNAL(sendCommandSignal(const QString&)),
            leftBaseTcp.get(), SLOT(onSend(QString)), Qt::QueuedConnection);
    connect(this, SIGNAL(sendCommandSignal2000(const QString&)),
            rightBaseTcp.get(), SLOT(onSend(QString)), Qt::QueuedConnection);

    formManager->settingForm->setCallback(std::bind(&ConnectManager::sendCommand, this, std::placeholders::_1));
    formManager->acionForm->setCallback(std::bind(&ConnectManager::sendCommand, this, std::placeholders::_1));

    formManager->settingForm->setCallback2000(std::bind(&ConnectManager::sendCommandTo2000, this, std::placeholders::_1));
    formManager->acionForm->setCallback2000(std::bind(&ConnectManager::sendCommandTo2000, this, std::placeholders::_1));

}

void ConnectManager::sendCommand(const QString &cmd)
{
    emit sendCommandSignal(cmd);
}

void ConnectManager::sendCommandTo2000(const QString &cmd)
{
    emit sendCommandSignal2000(cmd);
}

